<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a @click="changeBrand(item.id)" href="javascript:;" v-for="item in filterData.brands" :key="item.id"
          :class="{active:item.id===filterData.brands.selectedBrand}">{{item.name}}</a>
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{item.name}}</div>
      <div class="body">
        <a @click="changeGoods(item,goods.id)" href="javascript:;" v-for="goods in item.properties" :key="goods.id"
          :class="{active:goods.id===item.properties.selectedArr}">{{goods.name}}</a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category'

export default {
  name: 'SubFilter',
  setup(props, { emit }) {
    // 监听二级类目id变化获取筛选数据
    const filterData = ref(null)
    const filterLoading = ref(false)
    const route = useRoute()
    watch(() => route.params.id, newVal => {
      if (newVal && `/category/sub/${route.params.id}` === route.path) {
        filterLoading.value = true
        findSubCategoryFilter(route.params.id).then(data => {
          data.result.brands.selectedBrand = null
          data.result.brands.unshift({ id: null, name: '全部' })
          data.result.saleProperties.forEach(item => {
            item.properties.selectedArr = null
            item.properties.unshift({ id: null, name: '全部' })
          })
          filterData.value = data.result
          filterLoading.value = false
        })
      }
    }, {
      immediate: true
    })

    const getFilterParams = () => {
      const obj = { brandId: null, attrs: [] }
      // 返回选中品牌id
      obj.brandId = filterData.value.brands.selectedBrand
      // 返回筛选的品类
      filterData.value.saleProperties.forEach(item => {
        if (item.properties.selectedArr) {
          const prop = item.properties.find(prop => prop.id === item.properties.selectedArr)
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      if (obj.attrs.length === 0) obj.attrs = null
      return obj
    }

    const changeBrand = (brandId) => {
      if (filterData.value.brands.selectedBrand === brandId) return
      filterData.value.brands.selectedBrand = brandId
      emit('filter-change', getFilterParams())
    }
    const changeGoods = (item, goodsId) => {
      if (item.properties.selectedArr === goodsId) return
      item.properties.selectedArr = goodsId
      emit('filter-change', getFilterParams())
    }
    return {
      filterData,
      filterLoading,
      changeBrand,
      changeGoods
    }
  }
}
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;

  .item {
    display: flex;
    line-height: 40px;

    .head {
      width: 80px;
      color: #999;
    }

    .body {
      flex: 1;

      a {
        margin-right: 36px;
        transition: all .3s;
        display: inline-block;

        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}

.xtx-skeleton {
  padding: 10px 0;
}
</style>
